﻿package map 
{
	import Common.GameDefine;
	import Common.RenderManager;
	import Common.MapDirection;
	
	/**
	 * ...
	 * @author fox
	 */
	public class MapPos 
	{
		public var mapID:int = 0;
		public var mapGrid:MapGrid = null;
		
		
		public var desGrid:MapGrid = null;
		public var startGrid:MapGrid = null;
		
		public var boolMove:Boolean = false;
		public var time:Number = 0.0;
		public var direction:int = MapDirection.DIRECTCOUNT;
		
		public var moveSpeed:Number = 0.5; 
		
		
		public function get isMoving():Boolean
		{
			return boolMove;
		}
		
		public function MapPos() 
		{
			
		}
		
		public function init():void
		{
			mapGrid = new MapGrid();
			desGrid = new MapGrid();
			startGrid = new MapGrid();
		}
		
		public function release():void
		{
			mapGrid = null;
			desGrid = null;
			startGrid = null;
		}
		
		public static function GetDirect( pos1:MapGrid , pos2:MapGrid ):int
		{
			var x:int = pos1.x - pos2.x;
			var y:int = pos1.y - pos2.y;
			
			var direction:int = MapDirection.DIRECTCOUNT;
			
			if ( x > 0 && y > 0 ) 
				direction = MapDirection.SOUTHEAST;
			else if ( x > 0 && y == 0 ) 
				direction = MapDirection.EAST;
			else if ( x > 0 && y < 0 ) 
				direction = MapDirection.NORTHEAST;
			else if ( x == 0 && y < 0 ) 
				direction = MapDirection.NORTH;
			else if ( x < 0 && y < 0 ) 
				direction = MapDirection.NORTHWEST;
			else if ( x < 0 && y == 0 ) 
				direction = MapDirection.WEST;
			else if ( x < 0 && y > 0 ) 
				direction = MapDirection.SOUTHWEST;
			else if ( x == 0 && y > 0 ) 
				direction = MapDirection.SOUTH;
				
			return direction;
		}
		
		public function startMove( des:MapGrid ):void
		{
			startGrid.copy( mapGrid );
			desGrid.copy( des );
			
			boolMove = true;
			time = 0.0;
			
			direction = GetDirect( desGrid , startGrid );
		}
		
		public function endMove():void
		{
			boolMove = false;
			mapGrid.copy( desGrid );
			//trace( desGrid );
		}
		
		public function move( delay:Number ):void
		{
			if ( !boolMove ) 
			{
				return;
			}
			
			RenderManager.redraw = true;
			//trace( delay );
			//trace( delay );
			time += delay;
			var dis:Number = time * moveSpeed;
			
			switch ( direction ) 
			{
				case MapDirection.NORTH:
				{
					if ( dis > MapGrid.gridInclined ) 
					{
						endMove();
						//trace( mapGrid );
						return;
					}
					
					mapGrid.realPosX = startGrid.realPosX - MapGrid.gridPX * dis;
					mapGrid.realPosY = startGrid.realPosY - MapGrid.gridPY * dis;
					//mapGrid.x = dis < MapGrid.gridInclined * 0.5 ? startGrid.x : startGrid.x - 1;
					mapGrid.y = dis < MapGrid.gridInclinedHalf ? startGrid.y : startGrid.y - 1;
					
					
					//trace( mapGrid );
				}
				break;
				case MapDirection.SOUTH:
				{
					if ( dis > MapGrid.gridInclined ) 
					{
						endMove();
						//trace( mapGrid );
						return;
					}
					
					mapGrid.realPosX = startGrid.realPosX + MapGrid.gridPX * dis;
					mapGrid.realPosY = startGrid.realPosY + MapGrid.gridPY * dis;
					//mapGrid.x = dis < MapGrid.gridInclined * 0.5 ? startGrid.x : startGrid.x - 1;
					mapGrid.y = dis < MapGrid.gridInclinedHalf ? startGrid.y : startGrid.y + 1;
					
					//trace( mapGrid );
				}
				break;
				case MapDirection.WEST:
				{
					if ( dis > MapGrid.gridInclined ) 
					{
						endMove();
						//trace( mapGrid );
						return;
					}
					
					mapGrid.realPosX = startGrid.realPosX - MapGrid.gridPX * dis;
					mapGrid.realPosY = startGrid.realPosY + MapGrid.gridPY * dis;
					//mapGrid.x = dis < MapGrid.gridInclined * 0.5 ? startGrid.x : startGrid.x - 1;
					mapGrid.x = dis < MapGrid.gridInclinedHalf ? startGrid.x : startGrid.x - 1;
					
					//trace( mapGrid );
				}
				break;
				case MapDirection.EAST:
				{
					if ( dis > MapGrid.gridInclined ) 
					{
						endMove();
						//trace( mapGrid );
						return;
					}
					
					mapGrid.realPosX = startGrid.realPosX + MapGrid.gridPX * dis;
					mapGrid.realPosY = startGrid.realPosY - MapGrid.gridPY * dis;
					//mapGrid.x = dis < MapGrid.gridInclined * 0.5 ? startGrid.x : startGrid.x - 1;
					mapGrid.x = dis < MapGrid.gridInclinedHalf ? startGrid.x : startGrid.x + 1;
					
					//trace( mapGrid );
				}
				break;
				case MapDirection.NORTHWEST:
				{
					if ( dis > MapGrid.gridX ) 
					{
						endMove();
						//trace( mapGrid );
						return;
					}
					
					mapGrid.realPosX = startGrid.realPosX - dis;
					mapGrid.x = dis < MapGrid.gridXHalf ? startGrid.x : startGrid.x - 1;
					mapGrid.y = dis < MapGrid.gridXHalf ? startGrid.y : startGrid.y - 1;
				}
				break;
				case MapDirection.NORTHEAST:
				{
					if ( dis > MapGrid.gridY ) 
					{
						endMove();
						//trace( mapGrid );
						return;
					}
					
					mapGrid.realPosY = startGrid.realPosY - dis;
					mapGrid.x = dis < MapGrid.gridYHalf ? startGrid.x : startGrid.x + 1;
					mapGrid.y = dis < MapGrid.gridYHalf ? startGrid.y : startGrid.y - 1;
				}
				break;
				case MapDirection.SOUTHWEST:
				{
					if ( dis > MapGrid.gridY ) 
					{
						endMove();
						//trace( mapGrid );
						return;
					}
					
					mapGrid.realPosY = startGrid.realPosY + dis;
					mapGrid.x = dis < MapGrid.gridYHalf ? startGrid.x : startGrid.x - 1;
					mapGrid.y = dis < MapGrid.gridYHalf ? startGrid.y : startGrid.y + 1;
				}
				break;
				case MapDirection.SOUTHEAST:
				{
					if ( dis > MapGrid.gridX ) 
					{
						endMove();
						//trace( mapGrid );
						return;
					}
					
					mapGrid.realPosX = startGrid.realPosX + dis;
					mapGrid.x = dis < MapGrid.gridXHalf ? startGrid.x : startGrid.x + 1;
					mapGrid.y = dis < MapGrid.gridXHalf ? startGrid.y : startGrid.y + 1;
				}
				break;
				default:
					endMove();
					return;
				break
			}
				
		}
		
		
	}
	
}

